﻿//-----------------------------------------------------------------------
// <copyright file="EditCustomerViewModel.cs" company="FER">
//     Copyright (c) All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace NMBP.View.ViewModel
{
    using System;
    using System.Collections.Generic;

    using GalaSoft.MvvmLight;
    using GalaSoft.MvvmLight.Command;
    using GalaSoft.MvvmLight.Messaging;

    using NMBP.DAL;
    using NMBP.Models;
    using NMBP.Models.Enums;
    using NMBP.ViewModels.Dialogs;

    /// <summary>
    /// This class contains properties that the customer View can data bind to.
    /// <para>
    /// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel.
    /// </para>
    /// <para>
    /// You can also use Blend to data bind with the tool's support.
    /// </para>
    /// <para>
    /// See http://www.galasoft.ch/mvvm/getstarted
    /// </para>
    /// </summary>
    public class EditCustomerViewModel : ViewModelBase
    {
        #region Binding Fields
        /// <summary>
        /// Gets or sets customers name
        /// </summary>
        private string name;

        /// <summary>
        /// Gets or sets customers surname
        /// </summary>
        private string surname;

        /// <summary>
        /// Gets or sets customers username
        /// </summary>
        private string username;

        /// <summary>
        /// Gets or sets customers password
        /// </summary>
        private string password;

        /// <summary>
        /// Gets or sets customers contact
        /// </summary>
        private string contact;

        /// <summary>
        /// Gets or sets customers bank curd number
        /// </summary>
        private string bankCardNumber;

        /// <summary>
        /// Gets or sets selected payment method
        /// </summary>
        private string selectedPaymentMethod;

        /// <summary>
        /// Gets or sets list of available payment methods
        /// </summary>
        private List<string> availablePaymentMethod;

        /// <summary>
        /// Action raised when customer view model needs to be initialized
        /// </summary>
        private Action<Customer> initCustomerViewModel;
        #endregion

        /// <summary>
        /// Gets or sets recieved customer
        /// </summary>
        private Customer recievedCustomer;

        /// <summary>
        /// Initializes a new instance of the EditCustomerViewModel class
        /// </summary>
        public EditCustomerViewModel()
        {
            this.initCustomerViewModel = new Action<Customer>(this.InitializeViewModel);
            Messenger.Default.Register<Customer>(this, "InitializeEditCustomer", this.initCustomerViewModel);

            this.SaveEditCustomer = new RelayCommand(
                () =>
                    {
                        this.recievedCustomer.Name = this.Name;
                        this.recievedCustomer.Surname = this.Surname;
                        this.recievedCustomer.Username = this.Username;
                        this.recievedCustomer.Password = this.Password;
                        this.recievedCustomer.Contact = this.Contact;
                        this.recievedCustomer.BankCardNumber = this.BankCardNumber;
                        this.recievedCustomer.PrefferedPayment = this.DeterminePreferedPayment();

                        this.DbFacade.SaveCustomer(this.recievedCustomer);

                        Messenger.Default.Send(this.recievedCustomer, "CloseEditCustomerWindow");
                    });

            this.CancelEditCustomer = new RelayCommand(
                () =>
                    {
                        Messenger.Default.Send(this.recievedCustomer, "CloseEditCustomerWindow");
                    });
        }

        #region Bindable Properties
        /// <summary>
        /// Gets or sets dbFacade
        /// </summary>
        public IDbFacade DbFacade { get; set; }

        /// <summary>
        /// Gets or sets dialog service
        /// </summary>
        public IDialogService DialogService { get; set; }

        /// <summary>
        /// Gets or sets customers name
        /// </summary>
        public string Name
        {
            get
            {
                return this.name;
            }

            set
            {
                this.name = value;
                this.RaisePropertyChanged("Name");
            }
        }

        /// <summary>
        /// Gets or sets customers surname
        /// </summary>
        public string Surname
        {
            get
            {
                return this.surname;
            }

            set
            {
                this.surname = value;
                this.RaisePropertyChanged("Surname");
            }
        }

        /// <summary>
        /// Gets or sets customers username
        /// </summary>
        public string Username
        {
            get
            {
                return this.username;
            }

            set
            {
                this.username = value;
                this.RaisePropertyChanged("Username");
            }
        }

        /// <summary>
        /// Gets or sets customers password
        /// </summary>
        public string Password
        {
            get
            {
                return this.password;
            }

            set
            {
                this.password = value;
                this.RaisePropertyChanged("Password");
            }
        }

        /// <summary>
        /// Gets or sets customers contact
        /// </summary>
        public string Contact
        {
            get
            {
                return this.contact;
            }

            set
            {
                this.contact = value;
                this.RaisePropertyChanged("Contact");
            }
        }

        /// <summary>
        /// Gets or sets customers bank curd number
        /// </summary>
        public string BankCardNumber
        {
            get
            {
                return this.bankCardNumber;
            }

            set
            {
                this.bankCardNumber = value;
                this.RaisePropertyChanged("BankCardNumber");
            }
        }

        /// <summary>
        /// Gets or sets selected payment method
        /// </summary>
        public string SelectedPaymentMethod
        {
            get
            {
                return this.selectedPaymentMethod;
            }

            set
            {
                this.selectedPaymentMethod = value;
                this.RaisePropertyChanged("SelectedPaymentMethod");
            }
        }

        /// <summary>
        /// Gets or sets list of available payment methods
        /// </summary>
        public List<string> AvailablePaymentMethod
        {
            get
            {
                return this.availablePaymentMethod;
            }

            set
            {
                this.availablePaymentMethod = value;
                this.RaisePropertyChanged("AvailablePaymentMethod");
            }
        }
        #endregion

        #region Bindable commands
        /// <summary>
        /// Gets the command raised when save edit customer button is clicked
        /// </summary>
        public RelayCommand SaveEditCustomer { get; private set; }

        /// <summary>
        /// Gets the command raised when save edit customer button is clicked
        /// </summary>
        public RelayCommand CancelEditCustomer { get; private set; }
        #endregion

        /// <summary>
        /// Initializes view model
        /// </summary>
        /// <param name="customer">customer to initialize view model</param>
        private void InitializeViewModel(Customer customer)
        {
            if (this.DbFacade == null)
            {
                this.DbFacade = new DbFacade();
            }

            this.recievedCustomer = customer;
            this.DialogService = new DialogService();

            this.Name = customer.Name;
            this.Surname = customer.Surname;
            this.Username = customer.Username;
            this.Password = customer.Password;
            this.Contact = customer.Contact;
            this.BankCardNumber = customer.BankCardNumber;

            this.AvailablePaymentMethod = new List<string>() { "Gotovina", "Kartica" };
            this.SelectedPaymentMethod = customer.PrefferedPayment == PaymentMethod.Cash
                                             ? this.AvailablePaymentMethod[0]
                                             : this.AvailablePaymentMethod[1];
        }

        /// <summary>
        /// Determines selected payment method
        /// </summary>
        /// <returns>Payment method</returns>
        private PaymentMethod DeterminePreferedPayment()
        {
            if (this.SelectedPaymentMethod == "Gotovina")
            {
                return PaymentMethod.Cash;
            }

            return PaymentMethod.Card;
        }
    }
}
